<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    width="63%"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="标题" prop="title"
                :rules="[
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写标题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="内容" prop="content"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.content" placeholder="请填写内容"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="姓名" prop="name"
                :rules="[
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="邮箱" prop="email"
                :rules="[
                 ]">
              <el-input v-model="inputForm.email" placeholder="请填写邮箱"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="IP" prop="ip"
                :rules="[
                 ]">
              <el-input v-model="inputForm.ip" placeholder="请填写IP"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="公开标记" prop="openFlag"
                :rules="[
                 ]">
              <el-switch
                v-model="inputForm.openFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="回复人" prop="replyBy"
                :rules="[
                 ]">
              <el-input v-model="inputForm.replyBy" placeholder="请填写回复人"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="回复时间" prop="replyDate"
                :rules="[
                 ]">
              <el-date-picker
                v-model="inputForm.replyDate"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="设置回复时间">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="回复内容" prop="replyContent"
                :rules="[
                 ]">
              <el-input v-model="inputForm.replyContent" placeholder="请填写回复内容"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="置顶权重" prop="topWeight"
                :rules="[
                 ]">
              <el-input-number size="small" v-model="inputForm.topWeight" :min="1"></el-input-number>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="置顶期限" prop="topDate"
                :rules="[
                 ]">
              <el-date-picker
                v-model="inputForm.topDate"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="设置置顶期限">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="点赞数" prop="agree"
                :rules="[
                 ]">
              <el-input v-model="inputForm.agree" placeholder="请填写点赞数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="反对数" prop="oppose"
                :rules="[
                 ]">
              <el-input v-model="inputForm.oppose" placeholder="请填写反对数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import SelectTree from '@/components/treeSelect/treeSelect.vue'

export default {
  data () {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        title: '',
        content: '',
        name: '',
        email: '',
        ip: '',
        openFlag: '',
        replyBy: '',
        replyDate: '',
        replyContent: '',
        topWeight: '',
        topDate: '',
        agree: '',
        oppose: '',
        remarks: ''
      }
    }
  },
  components: {
    SelectTree
  },
  methods: {
    init (method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建留言表`
      } else if (method === 'edit') {
        this.title = '修改留言表'
      } else if (method === 'view') {
        this.title = '查看留言表'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/cms/guestbook/cmsGuestbook/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.cmsGuestbook)
            this.loading = false
          })
        }
      })
    },
      // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/cms/guestbook/cmsGuestbook/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
